package cn.qqncn.furniture.service;

import cn.qqncn.furniture.mapper.UserMapper;
import cn.qqncn.furniture.pojo.domain.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * @author zkw
 * @ClassName UserService.java
 * @Description user服务层
 * @createTime 2020年12月05日 23:05:00
 */
@Service
public class UserService {
    @Resource
    UserMapper userMapper;

    /**
     * 返回true，表示可以插入
     * @param username
     * @return
     */
    public boolean checkUsername(String username){

        User user = userMapper.selectByUserName(username);
        return user==null;
    }


    /**
     * 用户注册
     * @param user
     * @return 用户名已存在或注册失败返回flase
     */
    public boolean register(User user){
        User user1 = userMapper.selectByUserName(user.getUsername());
        if(user1==null){
            int insert = userMapper.insert(user);
            return insert>=1;
        }else{
            return false;
        }
    }

    /**
     * 登录
     * @param user
     * @return 用户对象，登录失败返回空
     */
    public User login(User user){
        User result = null;
        if(user!=null){
            User user1 = userMapper.selectByUserName(user.getUsername());
            if(user1 != null){
                if(user1.getPassword().equals(user.getPassword())){
                    result = user1;
                }
            }
        }
        return result;
    }

    /**
     * 查找所有用户
     * @return List
     */
    public List<User> searchAll(){
        List<User> user1 = userMapper.selectAllUser();
        return user1;
    }

}
